$(function(){
	function init(){
		var loginInfo = JSON.parse(decodeURI(decodeURI(localStorage.getItem('jf_login_info'))));
		if(loginInfo){
			$('#userCount').val(loginInfo.principal);
			$('#password').val(loginInfo.password);
			$('#remeberMe').prop('checked',true);
		}
	}
	init();
	//获取用户输入信息
	function getUserInfo(){
		var principal = $('#userCount').val().trim();//获取账户
		var password = $('#password').val().trim();//获取密码
		return {
			principal:principal,
			password:password
		}
	}
	//校验登录输入信息是否为空
	function checkInput(){
		if(getUserInfo().principal == '' || getUserInfo().password == ''){
			layer.alert('账号或密码不能为空', {icon: 6});
			return false;
		}
		return true;
	}
	//是否本地保存用户信息，记住我
	function remeberMe(){
		var isChecked = $('#remeberMe').prop('checked');
		if(isChecked){
			localStorage.setItem("jf_login_info",encodeURI(JSON.stringify(getUserInfo())));
		}else{
			localStorage.removeItem("jf_login_info");
		}
		return true;
	}

	function handleLogin(){
		if(remeberMe() && checkInput()){
			layer.load(0, {shade: 0.3});
			var postData = {
				principal: getUserInfo().principal,
				password: getUserInfo().password
			}
			function successFun(res){
				sessionStorage.setItem("jf_user_info", encodeURI(JSON.stringify(res.data)));
				window.location.href = 'html/billList.html'
			}
			_http.handleLogin(postData,successFun);
		}
	}
	//登录按钮点击事件
	$('#loginBtn').click(function(){
		handleLogin();
	})
	//键盘enter事件
	$(document).keydown(function(event){
		if(event.keyCode == 13){
			handleLogin();
		};
	})
})
